package 面试刷题.数字马力;

import java.util.Scanner;

public class ch04_字符替换 {

    /**
     * 思路：先将所有的ali替换成特殊字符。然后正常反转，反转后将ali替换回来
     * "welcome to alibaba!" -> "!ABABali OT EMOCLEW"
     * "ali all in, Ali ilA" -> "ALI ILA ,NI LLA ali"
     * "keep ali" -> "ali PEEK"
     * @param args
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (sc.hasNextLine()) { // 注意 while 处理多个 case
            String line = sc.nextLine();
            String newLine = invoke(line);
            System.out.println(line+" -> "+newLine);
        }
    }

    static String old = "@";

    public static String invoke(String str) {
        String newStr = str.replaceAll("ali", old);
        char[] chars = newStr.toCharArray();
        char[] newchars = new char[chars.length];
        for (int i = chars.length - 1, j = 0; i >= 0; i--, j++) {
            if (chars[i] >= 'a' && chars[i] <= 'z') {
                chars[i] -= 32;
            }
            newchars[j] = chars[i];
        }
        String s = new String(newchars);
        return s.replaceAll(old, "ali");
    }
}
